#include "oslib.h"
#include "lightband.h"

#define LB_HCAN     (&hcan2)
#define LB_CANID    (221)

#define LB_CNT      (4)

static lb_color_t lb_color_array[LB_CNT] = {(lb_color_t)0};
static lb_blink_t lb_blink_array[LB_CNT] = {(lb_blink_t)0};

void lb_task(void *argument)
{
    for (;;) {
        static CAN_Message msg;
        for (int i = 0; i < 4; i++) {
            msg.ui8[i * 2] = lb_color_array[i];
            msg.ui8[i * 2 + 1] = lb_blink_array[i];
        }
        OSLIB_CAN_SendMessage(LB_HCAN, CAN_ID_STD, LB_CANID, &msg);
        osDelay(50);
    }
}

/**
 * @brief 设置灯的状态
 * 
 * @param index 灯编号
 * @param color 灯颜色
 * @param blk   灯闪烁情况
 * @return int  0表示成功, -1表示参数错误
 */
int lb_set_light(int index, lb_color_t color, lb_blink_t blk)
{
    if (index < 0 || index >= LB_CNT) 
        return -1;
    if (color >= LB_COLOR_MAX)
        return -1;
    if (blk >= LB_BLINK_MAX)
        return -1;
    lb_color_array[index] = color;
    lb_blink_array[index] = blk;
    return 0;
}